package com.liyuhan.onlinetaxibackground.Service.impl;

import com.liyuhan.onlinetaxibackground.POJO.Car;
import com.liyuhan.onlinetaxibackground.POJO.Driver;
import com.liyuhan.onlinetaxibackground.POJO.Passenger;
import com.liyuhan.onlinetaxibackground.POJO.User;
import com.liyuhan.onlinetaxibackground.Repository.CarRepository;
import com.liyuhan.onlinetaxibackground.Repository.DriverRepository;
import com.liyuhan.onlinetaxibackground.Repository.PassengerRepository;
import com.liyuhan.onlinetaxibackground.Repository.UserRepository;
import com.liyuhan.onlinetaxibackground.Service.RegisterService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.Util.JedisPoolUtils;
import com.liyuhan.onlinetaxibackground.Util.TimeUtil;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class RegisterImpl implements RegisterService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PassengerRepository passengerRepository;
    @Autowired
    private DriverRepository driverRepository;
    @Autowired
    private CarRepository carRepository;

    // 响应体
    private DataResponse dataResponse;

    // 注册服务
    public DataResponse doRegister(String request) {
        // 解析请求体
        DataRequest dataRequest = GetRequestBody.getRequestBody(request);

        // 取出请求参数
        // 验证码
        String verifyCode = dataRequest.get("verifyCode").toString();
        // 手机号
        String phone = dataRequest.get("phone").toString();
        // 密码
        String password = dataRequest.get("password").toString();
        // 用户类型
        String type = dataRequest.get("type").toString();

        // 验证码检验
        if (!getRedisCode(phone, verifyCode)) {
            // 用户未通过验证
            Map<String, String> map = new HashMap<>();
            map.put("error", "用户验证码错误！！");
            dataResponse = new DataResponse("400", map);
            // 直接返回
            return dataResponse;
        }

        // 创建用户
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        // 设置注册日期
        user.setRegisterDate(TimeUtil.StringToDate(TimeUtil.DateToString(new Date())));
        // 设置用户身份
        user.setType(type);
        // 设置用户登录状态
        user.setState(false);

        try {
            // 添加到数据库
            userRepository.save(user);
        } catch (Exception e) {
            Map<String, String> map = new HashMap<>();
            map.put("error", "插入数据库错误！！");
            dataResponse = new DataResponse("400", map);
        }

        // 创建乘客/司机信息
        if (type.equals("passenger")) {
            Passenger passenger = new Passenger();
            passenger.setUser(user);
            // 保存乘客
            passengerRepository.save(passenger);
        } else if (type.equals("driver")) {
            // 获取车辆信息
            String carNumber = dataRequest.getData().get("carNumber").toString();
            String carColor = dataRequest.getData().get("carColor").toString();
            String carBrand = dataRequest.getData().get("carBrand").toString();
            // 创建车辆对象
            Car car = new Car(carNumber, carColor, carBrand);
            // 保存车辆对象
            carRepository.save(car);
            // 创建司机对象
            Driver driver = new Driver(car, user);
            // 保存司机
            driverRepository.save(driver);
        }

        Map<String, String> map = new HashMap<>();
        map.put("success", "用户注册成功！！");
        dataResponse = new DataResponse("200", map);

        return dataResponse;
    }

    //3 验证码校验
    public static boolean getRedisCode(String phone, String code) {
        //从redis获取验证码
        Jedis jedis = JedisPoolUtils.getJedis();

        if (jedis == null) {
            return false;
        }
        //验证码key
        String codeKey = "VerifyCode" + phone + ":code";
        String redisCode = jedis.get(codeKey);
        //判断
        if (redisCode.equals(code)) {
            // 从redis中清除当前验证码
            jedis.del(codeKey);

            jedis.close();
            return true;
        } else {
            jedis.close();
            return false;
        }
    }
}
